$(function(){
    initSubmitEvent();
    initForm();
});
function initSubmitEvent(){
    $('#submitForm').validate({
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function(form){
            if (typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') {
                jQuery.data(this, "disabledOnSubmit", {submited : true});
                //异步提交
                var requestJson = $(form).serializeJSON();
                var that = this;
                if(confirm("确认修改？")) {
                    $.ajax({
                        url: form.action,
                        type: "POST",
                        contentType: 'application/json;charset=utf-8', //设置请求头信息
                        dataType: "json",
                        data: JSON.stringify(requestJson),
                        success: function (data) {
                            if (data.code == 0) {
                                alert(data.msg);
                                window.location.href = appServer + "/quota/type/list.htm";
                            } else {
                                alert(data.msg);
                                jQuery.removeData(that, "disabledOnSubmit");
                            }
                        },
                        error: function (res) {
                            alert(res.responseText);
                            jQuery.removeData(that, "disabledOnSubmit");
                        }
                    });
                }else{
                    jQuery.removeData(that, "disabledOnSubmit");
                }
            } else {
                // repeat submit ignore;
            }
        },
        success:function(element){
        },
        errorPlacement : function(error, element) {
            if(error.text()){
                if(element.next("span[class='red']").size() == 0){
                    element.after("<span class='red'>" + error.text() +"</span>");
                }else{
                    element.next("span[class='red']").text(error.text());
                }
            }else{
                element.next("span[class='red']").remove();
            }
        },
        rules:{
            "name":{required:true},
            "tradeAmountLimit":{required:true,min:0,digits:true},
            "unfreezeCycle":{conditionRequired:(function(){
                    var flag = $('input[name="isNeedFreeze"]:checked').val();
                    if(flag != "1"){
                        var unfreezeCycle = $("[name=unfreezeCycle]").val();
                        unfreezeCycle = unfreezeCycle.replace(/(^\s*)|(\s*$)/g, '');//去除空格
                        if( unfreezeCycle == undefined || unfreezeCycle == "" || unfreezeCycle == null){
                            return true;
                        }
                    }
                    return false;
                }),conditionIsIntGtZero:(function(){
                    var flag = $('input[name="isNeedFreeze"]:checked').val();
                    if(flag != "1"){
                        return true;
                    }
                    return false;
                })},
            "unfreezeNumInTimes":{conditionRequired:(function(){
                    var flag = $('input[name="isNeedFreeze"]:checked').val();
                    if(flag != "1"){
                        var unfreezeNumInTimes = $("[name=unfreezeNumInTimes]").val();
                        unfreezeNumInTimes = unfreezeNumInTimes.replace(/(^\s*)|(\s*$)/g, '');//去除空格
                        if( unfreezeNumInTimes == undefined || unfreezeNumInTimes == "" || unfreezeNumInTimes == null){
                            return true;
                        }
                    }
                    return false;
                }),conditionIsIntGtZero:(function(){
                    var flag = $('input[name="isNeedFreeze"]:checked').val();
                    if(flag != "1"){
                        return true;
                    }
                    return false;
                })},
            "exchangeAmount":{required:true,isIntGtZero:true}
        },
        messages:{
            "name":{required:"商品名称必填"},
            "unfreezeCycle":{conditionRequired:"解冻周期必填",conditionIsIntGtZero:"只能输入大于0的正整数"},
            "unfreezeNumInTimes":{conditionRequired:"每次解冻数量必填",conditionIsIntGtZero:"只能输入大于0的正整数"},
            "exchangeAmount":{required:"每次兑换数量必填",isIntGtZero:"只能输入大于0的正整数"}
        }
    });
}

function initForm() {
    var flag = $('input[name="isNeedFreeze"]:checked').val();
    if (flag != "1") {
        show();
    }else {
        hide();
    }
}

function show() {
    $('.dynamic').show();
}

function hide() {
    $('.dynamic').hide();
}